package com.hushui.mall.commodity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hushui.common.utils.PageUtils;
import com.hushui.common.utils.Query;
import com.hushui.mall.commodity.dao.ProductAttrValueDao;
import com.hushui.mall.commodity.entity.AttrEntity;
import com.hushui.mall.commodity.entity.ProductAttrValueEntity;
import com.hushui.mall.commodity.service.AttrService;
import com.hushui.mall.commodity.service.ProductAttrValueService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {
    @Resource
    AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> findAttrBySpuId(Long spuId) {
        //根据spuid查询所有的属性
        List<ProductAttrValueEntity> spu_id = this.list(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
        //获取所有属性的id去属性表查属性
        List<Long> collect = spu_id.stream().map(ProductAttrValueEntity::getAttrId).collect(Collectors.toList());
        List<AttrEntity> attrEntities = attrService.listByIds(collect);
        //获取允许查询的属性名
        List<AttrEntity> collect1 = attrEntities.stream().filter(attrEntity -> attrEntity.getSearchType() == 1).collect(Collectors.toList());
        return collect1;
    }

    @Override
    public List<ProductAttrValueEntity> baseAttrListforspu(Long spuId) {

        List<ProductAttrValueEntity> attrValueEntityList = this.baseMapper.selectList(
                new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));

        return attrValueEntityList;
    }

}